package org.loofer.ext.utils;

import org.loofer.ext.component.logger.Logger;
import java.lang.reflect.Field;

public final class ReflectUtils {
    private static final String TAG = "ReflectUtils";

    private ReflectUtils() {
    }

    public static Object getField(String className, String fieldName, Object instance) {
        try {
            return getField(Class.forName(className), fieldName, instance);
        } catch (Throwable th) {
            Logger.i(TAG, "fail to get field " + fieldName + " with " + instance + " from " + className);
            return null;
        }
    }

    public static Object getField(Class<?> clazz, String fieldName, Object instance) {
        try {
            Field field = clazz.getField(fieldName);
            field.setAccessible(true);
            return field.get(instance);
        } catch (Throwable th) {
            Logger.i(TAG, "fail to get field " + fieldName + " with " + instance + " from " + clazz);
            return null;
        }
    }

    public static boolean setField(String className, String fieldName, Object instance, Object value) {
        try {
            return setField(Class.forName(className), fieldName, value, instance);
        } catch (Throwable th) {
            Logger.i(TAG, "fail to set field " + fieldName + " to " + value + " with " + instance + " from " + className);
            return false;
        }
    }

    public static boolean setField(Class<?> clazz, String fieldName, Object instance, Object value) {
        try {
            Field field = clazz.getField(fieldName);
            field.setAccessible(true);
            field.set(instance, value);
            return true;
        } catch (Throwable th) {
            Logger.i(TAG, "fail to set field " + fieldName + " to " + value + " with " + instance + " from " + clazz);
            return false;
        }
    }

    public static Object invokeMethod(String className, String methodName, Object instance, Object[] args) {
        return invokeMethod(className, methodName, assumeArgumentTypes(args), instance, args);
    }

    public static Object invokeMethod(Class<?> clazz, String methodName, Object instance, Object[] args) {
        return invokeMethod((Class) clazz, methodName, assumeArgumentTypes(args), instance, args);
    }

    public static Object invokeMethod(String className, String methodName, Class<?>[] argsTypes, Object instance, Object[] args) {
        try {
            return invokeMethod(Class.forName(className), methodName, (Class[]) argsTypes, instance, args);
        } catch (Throwable th) {
            Logger.i(TAG, "fail to invoke method " + methodName + " with " + instance + " from " + className);
            return null;
        }
    }

    public static Object invokeMethod(Class<?> clazz, String methodName, Class<?>[] argsTypes, Object instance, Object[] args) {
        try {
            return clazz.getMethod(methodName, argsTypes).invoke(instance, args);
        } catch (Throwable th) {
            Logger.i(TAG, "fail to invoke method " + methodName + " with " + instance + " from " + clazz);
            return null;
        }
    }

    private static Class<?>[] assumeArgumentTypes(Object[] args) {
        Class<?>[] argsTypes = null;
        if (args != null && args.length > 0) {
            argsTypes = new Class[args.length];
            int length = args.length;
            int i = 0;
            int i2 = 0;
            while (i < length) {
                Object obj = args[i];
                if (obj == null) {
                    throw new IllegalArgumentException("Null argument is not permitted for automatic argument class type assumption.");
                }
                int i3 = i2 + 1;
                argsTypes[i2] = obj.getClass();
                i++;
                i2 = i3;
            }
        }
        return argsTypes;
    }
}
